package com.platform.web.lisenter;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.system.service.ISysDictDataService;
import com.platform.system.utils.DictUtils;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class WeatherListener {

    @Autowired
    private ISysDictDataService sysDictDataService;

    @Autowired
    private RedisUtil redisUtil;

    @RabbitListener(queues = "weather_result")
    public void receiveValve(JSONObject jsonObject, Channel channel, Message message){
        try {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            String messageId = message.getMessageProperties().getHeaders().get("spring_returned_message_correlation").toString();
            if (redisUtil.hasKey("message:" + messageId)) {
                return;
            }
            redisUtil.set("message:"+messageId, messageId, 1000);
            String menon = jsonObject.getString("001");
            String temp = jsonObject.getString("002") + "℃";
            String power = jsonObject.getString("003");
            String direction = jsonObject.getString("004");
            String damp = jsonObject.getString("005") + "%";
            String phenomenon = sysDictDataService.selectDictLabel("phenomenon", menon);
            power = sysDictDataService.selectDictLabel("power", power);
            direction = sysDictDataService.selectDictLabel("direction", direction);

            String msg = " 气温：" + temp + " 风力：" + power + " 风向：" + direction + " 湿度：" + damp;

            String pheno = "/platform/weather/" + menon + phenomenon + ".png";

            Map map = new HashMap<>();
            map.put("message", msg);
            map.put("pheno", pheno);

            redisUtil.hmset("observe", map);
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}
